package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.WorksheetModel;
import com.xieyingwangluo.yxpart.service.WebSocket;
import com.xieyingwangluo.yxpart.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class WorkLogic {

    @Autowired
    private WorkService workService;
    @Autowired
    private WebSocket webSocket;

    //模糊查询所有
    public Result selA(WorksheetModel work) {
        if (work != null) {
            List<WorksheetModel> list = workService.selA(work);
            return Result.generate(0, "Select success", list);
        }
        return Result.generate(1, "Select fail", null);
    }

    //根据id获取工作信息
    public Result getById(String noid) {
        if (noid != null || !"".equals(noid)) {
            WorksheetModel work = workService.getById(noid);
            return Result.generate(0, "Select success", work);
        }
        return Result.generate(1, "Select fail", null);
    }

    //根据登录用户和工作状态查询工作列表
    //工作状态(0已报名，1已录用，2已完工)
    public Result getByState(String noid, int state) {
        List<WorksheetModel> list = workService.getByState(noid, state);
        if (list.size() > 0) {
            return Result.generate(0, "Select success", list);
        }
        return Result.generate(1, "Select fail", null);
    }

    //根据工作id修改工作状态（修改为录用）
    public Result updS(String noid, int state) {
        //工作状态(0已报名，1已录用，2,工作中，3已完工)

        if (noid != null || !"".equals(noid)) {
            int cnt = workService.updS(noid, state, new Date());
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    // 确认完工
    public Result updSByUser(String noid) {

        //工作状态(0已报名，1已录用，2,工作中，3已完工)
        if (noid != null || !noid.equals("")) {
            int state = 3;
            int cnt = workService.updSByUser(noid, state, new Date());
            if (cnt > 0) {
                return Result.generate(0, "Upd success", cnt);
            }
        }
        return Result.generate(1, "Upd fail", null);
    }

    //更新工作（修改工作情况）
    public Result update(WorksheetModel work) {
        WorksheetModel worksheetModel = workService.getById(work.getNoid());
        work.setPart_id(worksheetModel.getPart_id());
        work.setUserinfo_id(work.getUserinfo_id());
        work.setEnroll_id(worksheetModel.getEnroll_id());
        work.setRecord_time(worksheetModel.getRecord_time());
        work.setMakespan(worksheetModel.getMakespan());

        int cnt = workService.update(work);
        if (cnt > 0) {
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    //根据工作id修改工作状态（修改为录用）
    public Result updState(String part_id, String userinfo_id, int state) {
        //工作状态(0已报名，1已录用，2,工作中（已到岗），3已完工（已结算）)
        WorksheetModel work = workService.getWorkByU(part_id, userinfo_id);

        if (work != null) {
            int cnt = workService.updS(work.getNoid(), state, new Date());
            webSocket.sendMessage(part_id);
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }
}
